/**
  ******************************************************************************
  * @file
  * @brief 静态法主流程控制
  * @author luffy
  * @version V1.0.0
  * @date 2022/12/14
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "func_pid.h"
#include "step_motor.h"
#include "AD7193.h"		
#include "FreeRTOS.h"
#include "func_pt100.h"
#include "pfc_task.h"
#include "valve_ctl_task.h"
#include "lncup_ctl_task.h"
#include "dev_adg1404.h"
#include "pressuresensor.h"
#include "ParmManage.h"
#include "task.h"
#include "SYS.h"
/* Private defines -----------------------------------------------------------*/

/* Private Struct  -----------------------------------------------------------*/

/* Private enum    -----------------------------------------------------------*/

/* Private Variable  ---------------------------------------------------------*/
PFCCTRL           pfcCtrl;                       //静态法主流程控制参数
TEMP              temp;                          //温度参数
static int        s_pfc_state   = 0;             //静态法主流程控制状态
static float      s_P1   = 0;                    //基体腔充气前压力
static float      s_P2   = 0;                    //基体腔充气后压力
//Pointer

//Array

//Const

/***********************************************************************************
 * @brief 体积标定函数
 * ex:
 * @par  type：标定类型 num：标定次数
 * None
 * @retval
 **********************************************************************************/
void volumeCailbrate(uint8_t type, uint8_t num)
{
	
	return;
}

/***********************************************************************************
 * @brief 吸附P/Po函数
 * ex:
 * @par  type：P/Po实验类型 num：次数
 * None
 * @retval
 **********************************************************************************/
void adsorbPPo(uint8_t type, uint8_t cubeType, uint8_t num)
{
	
	return;
}

/***********************************************************************************
 * @brief 检测大气压模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void checkAtmoPressure(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres = 0;
	
	//打开电磁阀02 12 11
	//读取基体腔压力值pres
	
	BODYPRESCTRL.bleedPid.SetVal = pfcCtrl.aeratPres;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;
	
	//打开电磁阀26
	//读取基体腔压力值pres，计算标准差
	//DEL<=0.01
	
	//读取基体腔压力值pres计算出大气压pfcCtrl.airPres
	
	//关闭电磁阀26、11、12
	
	return;
}

/***********************************************************************************
 * @brief 标定抽真空模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void cailVacuumPump(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres = 0;
	
	//打开机械泵20 等待5s
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	//打开电磁阀02、21 等待2s
	vTaskDelay(pdMS_TO_TICKS(2000));
	
	//开关电磁阀07 12 11
	//读取基体腔压力值pres
	vTaskDelay(pdMS_TO_TICKS(1500));
	
	//打开电磁阀11 等待0.666s
	vTaskDelay(pdMS_TO_TICKS(666));
	
	BODYPRESCTRL.bleedPid.SetVal = 20000;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//打开电磁阀12、07
	BODYPRESCTRL.bleedPid.SetVal = 4000;
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//打开分子泵管路（10），关闭电磁阀11、07、12
	BODYPRESCTRL.presCtrlEn = 0;
	
	//读取基体腔压力值pres(直到<=30Pa)
	
	if(pfcCtrl.caliVacuum.moleEnable)
	{
		//打开分子泵40
		//读取基体腔压力值pres(直到压力和时间到)
	}
	else
	{
		//延时
	}
	
	//关闭电磁阀10、02、21
	
	//AdjustZero（P2P3）；读取P1 P2P3，计算Padjust
	//读取温度3
	
	//打开电磁阀07、05、27、24
	
	if(pfcCtrl.caliVacuum.leakEnable)
	{
		//循环读取基体腔压力值pres
		//计算Starting pressure和end pressure,得出漏气率LeakRateVm(是否加入协议)
		
		//打开电磁阀02、21
		
		//循环读取基体腔压力值pres
		//计算Starting pressure和end pressure,得出漏气率LeakRateVm(是否加入协议)
	}
	else
	{
		//延时
	}
	
	//关闭电磁阀07 05 2724，打开10 02 21，继续抽真空
	
	//读取基体腔压力值pres(直到压力和时间到)
	
	//关闭电磁阀10 0221，关闭分子泵（40）
	
	return;
}

/***********************************************************************************
 * @brief 常温自由体积标定模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void normalTempFreeVolCail(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres = 0;
	
	//打开电磁阀02、12、11
	//读取基体腔压力值pres
	
	//开关电磁阀15、24、12、11，通入He
	BODYPRESCTRL.bleedPid.SetVal = 12000;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;
	
	//关闭电磁阀02 21，打开02
	//读取基体腔压力值pres，计算离散程度
	//读取压力和温度
	
	//关闭电磁阀02
	//读取基体腔压力值pres，计算离散程度
	//读取压力和温度
	
	//打开电磁阀21
	//读取基体腔压力值pres，计算离散程度
	//读取压力和温度
	
	//关闭电磁阀21
	//读取基体腔压力值pres，计算离散程度
	//读取压力和温度
	
	volumeCailbrate(CAIL_P0,2);   //标定压力点？
	volumeCailbrate(CAIL_S1,5);
	
	//关闭所有电磁阀
	
	return;
}

/***********************************************************************************
 * @brief 冷自由体积标定模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void coldFreeVolCail(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres, res;
	
	s_LNCTRL.enable = 1;   //启动液氮面电机
	
	ADG1404_ON_CH(ADG1404_S1);
	res = adc7193.Read(&adc7193,AD7193_CH0,3); //
	
	//电压转电阻值res
	
	PT100_R2T(temp.ln, &res);
	
	while(temp.ln > -159.3f)
	{
		if(temp.ln < -20.0f)
		{
			//打开样品管和Po管
		}
		
		ADG1404_ON_CH(ADG1404_S1);
		res = adc7193.Read(&adc7193,AD7193_CH0,3); //
	
		//电压转电阻值res
		
		PT100_R2T(temp.ln, &res);
		
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//关闭样品管和Po管
	
	//打开电磁阀02
	//读取基体腔压力值pres
	
	//关闭电磁阀02
	//读取基体腔压力值pres
	
	//打开电磁阀21
	//读取基体腔压力值pres
	
	//关闭电磁阀21
	//读取基体腔压力值pres
	
	volumeCailbrate(CAIL_P0,2);
	volumeCailbrate(CAIL_S1,6);
	
	s_LNCTRL.enable = 0;   //关闭液氮面控制
	//关闭所有电磁阀
	
	return;
}

/***********************************************************************************
 * @brief 吸附抽真空模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void adsorVacuumPump(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres = 0;
	
	//打开机械泵20 等待5s
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	//开关电磁阀07 12 11
	//读取基体腔压力值pres
	vTaskDelay(pdMS_TO_TICKS(1500));
	
	//打开电磁阀11 
	
	BODYPRESCTRL.bleedPid.SetVal = 20000;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//打开电磁阀12、07
	BODYPRESCTRL.bleedPid.SetVal = 4000;
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//打开分子泵管路（10），关闭电磁阀11、07、12
	BODYPRESCTRL.presCtrlEn = 0;
	
	//读取基体腔压力值pres(直到<=30Pa)
	
	if(pfcCtrl.caliVacuum.moleEnable)
	{
		//打开分子泵40
		//读取基体腔压力值pres(直到压力和时间到)
	}
	else
	{
		//延时
	}
	
	//关闭分子泵和机械泵，电磁阀10、02、21
	
	//AdjustZero（P2P3）；读取P1 P2P3，计算Padjust
	
	//关闭所有电磁阀
	
	return;
}

/***********************************************************************************
 * @brief 吸脱附模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void adsorbDesorb(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres = 0;
	
	//样品吸附充气
	//打开电磁阀02
	//读取基体腔压力值pres，计算离散程度
	//读取压力和温度
	
	//关闭电磁阀02
	//读取基体腔压力值pres，计算离散程度
	//读取压力和温度
	
	adsorbPPo(ADSOR_INHALE,CAIL_P0,2);   
	adsorbPPo(ADSOR_INHALE,CAIL_S1,5);
	
	s_LNCTRL.enable = 1;   //启动液氮面电机
	//关闭所有电磁阀
	
	//样品吸附抽气
	//打开机械泵20 等待30s
	vTaskDelay(pdMS_TO_TICKS(30000));
	
	//读取基体腔压力值和温度
	
	adsorbPPo(ADSOR_INFALT,CAIL_P0,2); 
	
	//关闭电磁阀02
	//读取基体腔压力值
	
	//开关电磁阀22 2705 12 11，向基体腔通入N2
	BODYPRESCTRL.bleedPid.SetVal = 50000;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//打开电磁阀21
	//开关电磁阀07 12 11
	BODYPRESCTRL.bleedPid.SetVal = 30000;
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;
	
	//关闭电磁阀21、机械泵
	
	return;
}

/***********************************************************************************
 * @brief 回填充气模块函数
 * ex:
 * @par  enable：表示该模块是否执行
 * None
 * @retval
 **********************************************************************************/
void backFill(uint8_t enable)
{
	if(enable == 0)
		return;
	
	float pres = 0;
	
	//开关电磁阀02 21
	//读取基体腔压力值pres
	
	s_LNCTRL.enable = 0;   //关闭液氮面电机
	
	//关闭电磁阀12 26 11，打开11 12 
	
	//开关电磁阀22 27 05
	BODYPRESCTRL.bleedPid.SetVal = 90000;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//关闭电磁阀12、11、02、12
	BODYPRESCTRL.presCtrlEn = 0;
	
	return;
}

/***********************************************************************************
 * @brief 主流程自检函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void PFC_StatePost(void)
{
	pfcCtrl.devState = PFC_STATE_POST;
	
	//设备状态监测
	//关闭电磁阀
	
	s_pfc_state = PFC_STATE_IDLE;
	return;
}

/***********************************************************************************
 * @brief 主流程空闲函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void PFC_StateIdle(void)
{
	uint8_t cmd = 0;
	
	pfcCtrl.devState = PFC_STATE_IDLE;
	
	while(1)
	{
		vTaskDelay(pdMS_TO_TICKS(100));
		
		//异常状态检测，若异常进入PFC_STATE_ERROR
		
		cmd = pfcCtrl.runCtrl;
		
		switch(cmd)
        {
			case PFC_CMD_MAIN:
                s_pfc_state = PFC_STATE_RUN;
				return;
                break;
//			case PFC_CMD_CAIL:
//                s_pfc_state = PFC_STATE_RUN;
//				return;
//                break;
			case PFC_CMD_DEBUG:
                s_pfc_state = PFC_STATE_DEBUG;
				return;
                break;
        }
		
	}
}

/***********************************************************************************
 * @brief 主流程控制函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void PFC_StateRun(void)
{
	pfcCtrl.devState = PFC_STATE_RUN;
	
	//读取压力值s_P1（pressure.c提供多压力读取接口）
	
	//通过实验类型配置实验流程
	checkAtmoPressure(pfcCtrl.experiType.airPre);            //检测大气压模块
	
	cailVacuumPump(pfcCtrl.experiType.cailVac);              //标定抽真空模块
	
	normalTempFreeVolCail(pfcCtrl.experiType.normalTCail);   //常温自由体积标定模块
	
	coldFreeVolCail(pfcCtrl.experiType.coldTCail);           //冷自由体积标定模块
	
	adsorVacuumPump(pfcCtrl.experiType.adsorVac);            //吸脱附抽真空模块
	
	adsorbDesorb(pfcCtrl.experiType.adsorb);                 //吸脱附实验模块
	
	backFill(pfcCtrl.experiType.backFill);                   //回填充气模块
	
	//关闭分子泵和机械泵
	//关闭所有电磁阀
	
	s_pfc_state = PFC_STATE_IDLE;
	return;
}

/***********************************************************************************
 * @brief 主流程错误函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void PFC_StateError(void)
{

	return;
}

#define  TEST  1
#define  VALVEPRO  0  //1：比例阀测试流程
#define  BLEED  3  //0：放气 1：抽气 2：充气
/***********************************************************************************
 * @brief 主流程DEBUG函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void PFC_StateDebug(void)
{
#if 0 ///
	//打开机械泵20 等待5s
	mon_pumpTim_switch = true;  //开启计时
	
#if BLEED == 1
	//抽气 //打开机械泵、电磁阀13、10、11
	SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
	SWITCH_ON_OFF(CCS_SEL, SWITCH_OFF);
	SWITCH_ON_OFF(IO_13, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));
	dac8830.Write(&dac8830,0.0);
	vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_20, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));	
	SWITCH_ON_OFF(IO_05, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	
	BODYPRESCTRL.bleedPid.SetVal = 670;  //抽气目标速率
	BODYPRESCTRL.pidSwitch = 0;
	BODYPRESCTRL.presCtrlEn = 1;
#elif BLEED == 0
	//放气(放到大气压)
	SWITCH_ON_OFF(IO_26, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
#elif BLEED == 2
	//充气
	SWITCH_ON_OFF(IO_13, SWITCH_OFF);
	SWITCH_ON_OFF(IO_10, SWITCH_OFF);
	SWITCH_ON_OFF(IO_05, SWITCH_OFF);
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	SWITCH_ON_OFF(CCS_SEL, SWITCH_ON); 
#endif
	
#if VALVEPRO == 1
	//充气
	SWITCH_ON_OFF(IO_13, SWITCH_OFF);
	SWITCH_ON_OFF(IO_10, SWITCH_OFF);
	SWITCH_ON_OFF(IO_05, SWITCH_OFF);
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	SWITCH_ON_OFF(CCS_SEL, SWITCH_ON);
	
	BODYPRESCTRL.blowPid.SetVal = 670;  //充气气目标速率
	BODYPRESCTRL.pidSwitch = 1;
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(pBody.P_sensor1 <= 150000)
	{	
		if(pBody.P_sensor1 >= 80000)
		{
			BODYPRESCTRL.presCtrlEn = 0;	
			SWITCH_ON_OFF(IO_22, SWITCH_OFF);
			SWITCH_ON_OFF(IO_24, SWITCH_OFF);
			SWITCH_ON_OFF(IO_11, SWITCH_OFF);
			
			vTaskDelay(pdMS_TO_TICKS(10000));
			break;
		}
		
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	//抽气 //打开机械泵、电磁阀13、10、11
	SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
	SWITCH_ON_OFF(CCS_SEL, SWITCH_OFF);
	SWITCH_ON_OFF(IO_13, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));
	dac8830.Write(&dac8830,0.0);
	vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_20, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));	
	SWITCH_ON_OFF(IO_05, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	
	BODYPRESCTRL.bleedPid.SetVal = 670;  //抽气目标速率
	BODYPRESCTRL.pidSwitch = 0;
	BODYPRESCTRL.presCtrlEn = 1;
	
//	while(pBody.P_sensor1 >= 0)
//	{	
//		if(pBody.P_sensor1 <= 670)
//		{
//			dac8830.Write(&dac8830,0.0);
//	
//			//SWITCH_ON_OFF(IO_10, SWITCH_ON);
//		}
//		
//		vTaskDelay(pdMS_TO_TICKS(10));
//	}
#endif	

#if TEST == 1
	BODYPRESCTRL.presCtrlEn = 0;
	//抽气 //打开机械泵、电磁阀13、10、11
	SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_26, SWITCH_OFF);
	
	SWITCH_ON_OFF(CCS_SEL, SWITCH_ON);
	SWITCH_ON_OFF(IO_20, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_10, SWITCH_ON);
	//vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	//vTaskDelay(pdMS_TO_TICKS(1000));
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	dac8830.Write(&dac8830,2.2,0);
	vTaskDelay(pdMS_TO_TICKS(1000));
	
	while(pBody.P_sensor1 >= 0)
	{	
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	dac8830.Write(&dac8830, 0.0, 0);
	
	vTaskDelay(pdMS_TO_TICKS(10000));
	
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	while(pBody.P_sensor1 >= 0)
	{	
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	vTaskDelay(pdMS_TO_TICKS(1000));
	
	SWITCH_ON_OFF(IO_20, SWITCH_OFF);
	SWITCH_ON_OFF(IO_10, SWITCH_OFF);
	vTaskDelay(pdMS_TO_TICKS(2000));
	
	//充气
	SWITCH_ON_OFF(IO_13, SWITCH_OFF);
	SWITCH_ON_OFF(IO_10, SWITCH_OFF);
	SWITCH_ON_OFF(IO_05, SWITCH_OFF);
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	SWITCH_ON_OFF(CCS_SEL, SWITCH_ON);
	
	BODYPRESCTRL.blowPid.SetVal = 3000;  //抽气目标速率（实际比例阀最大4000）
	BODYPRESCTRL.pidSwitch = 1;
	BODYPRESCTRL.presCtrlEn = 1;
	
//	while(pBody.P_sensor1 <= 150000)
//	{	
//		if(pBody.P_sensor1 >= 100000)
//		{
//			BODYPRESCTRL.presCtrlEn = 0;	
//			SWITCH_ON_OFF(IO_22, SWITCH_OFF);
//			SWITCH_ON_OFF(IO_24, SWITCH_OFF);
//			SWITCH_ON_OFF(IO_11, SWITCH_OFF);
//			
//			vTaskDelay(pdMS_TO_TICKS(10000));
//			break;
//		}
//		
//		vTaskDelay(pdMS_TO_TICKS(10));
//	}
	
	while(pBody.P_sensor1 <= 20000)
	{			
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;	
	//SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
//	vTaskDelay(pdMS_TO_TICKS(1000));
	BODYPRESCTRL.presCtrlEn = 1;	
	
	while(pBody.P_sensor1 <= 40000)
	{			
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;	
	//SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
//	vTaskDelay(pdMS_TO_TICKS(2000));
	BODYPRESCTRL.presCtrlEn = 1;	
	
	while(pBody.P_sensor1 <= 60000)
	{			
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;	
	//SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	BODYPRESCTRL.presCtrlEn = 1;
	
	while(pBody.P_sensor1 <= 80000)
	{			
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;	
	//SWITCH_ON_OFF(IO_22, SWITCH_OFF);
	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
	vTaskDelay(pdMS_TO_TICKS(5000));
	
	SWITCH_ON_OFF(IO_22, SWITCH_ON);
	SWITCH_ON_OFF(IO_24, SWITCH_ON);
	SWITCH_ON_OFF(IO_11, SWITCH_ON);
	BODYPRESCTRL.presCtrlEn = 1;
	
//	while(pBody.P_sensor1 <= 70000)
//	{			
//		vTaskDelay(pdMS_TO_TICKS(10));
//	}
//	
//	BODYPRESCTRL.presCtrlEn = 0;	
//	//SWITCH_ON_OFF(IO_22, SWITCH_OFF);
//	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
//	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
//	vTaskDelay(pdMS_TO_TICKS(2000));
//	
//	SWITCH_ON_OFF(IO_22, SWITCH_ON);
//	SWITCH_ON_OFF(IO_24, SWITCH_ON);
//	SWITCH_ON_OFF(IO_11, SWITCH_ON);
//	BODYPRESCTRL.presCtrlEn = 1;
//	
//	while(pBody.P_sensor1 <= 80000)
//	{			
//		vTaskDelay(pdMS_TO_TICKS(10));
//	}
//	
//	BODYPRESCTRL.presCtrlEn = 0;	
//	//SWITCH_ON_OFF(IO_22, SWITCH_OFF);
//	SWITCH_ON_OFF(IO_24, SWITCH_OFF);
//	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
//	vTaskDelay(pdMS_TO_TICKS(2000));
//	
//	SWITCH_ON_OFF(IO_22, SWITCH_ON);
//	SWITCH_ON_OFF(IO_24, SWITCH_ON);
//	SWITCH_ON_OFF(IO_11, SWITCH_ON);
//	BODYPRESCTRL.presCtrlEn = 1;
	
	while(pBody.P_sensor1 <= 150000)
	{	
		if(pBody.P_sensor1 >= 100000)
		{
			BODYPRESCTRL.presCtrlEn = 0;	
			SWITCH_ON_OFF(IO_22, SWITCH_OFF);
			SWITCH_ON_OFF(IO_24, SWITCH_OFF);
			SWITCH_ON_OFF(IO_11, SWITCH_OFF);
			
			vTaskDelay(pdMS_TO_TICKS(10000));
			break;
		}
		
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	while(1)
	{
		vTaskDelay(pdMS_TO_TICKS(10));
	}

#endif	
	
	//无限制抽气(基体腔压力大于670Pa时比例阀全开)
//	while(pBody.P_sensor1 <= 110000)
//	{
//		if(pBody.P_sensor1 > 10000)
//		{
//			dac8830.Write(&dac8830,1.0);
//		}
//		
//		if(pBody.P_sensor1 <= 10000 && pBody.P_sensor1 >= 5000)
//		{
//			dac8830.Write(&dac8830,1.4);
//		}
//		
//		if(pBody.P_sensor1 < 5000 && pBody.P_sensor1 >= 2000)
//		{
//			dac8830.Write(&dac8830,1.8);
//		}
//		
//		if(pBody.P_sensor1 < 2000 && pBody.P_sensor1 >= 1000)
//		{
//			dac8830.Write(&dac8830,2.2);
//		}
//		
//		if(pBody.P_sensor1 < 1000)
//		{
//			dac8830.Write(&dac8830,0.0);
//		
//			SWITCH_ON_OFF(IO_10, SWITCH_ON);
//		}
//		
//		vTaskDelay(pdMS_TO_TICKS(10));
//	}
	
	while(BODYPRESCTRL.presStable == false)  //超时检测？
	{
		if(g_pumpCnt >= 60)
			break;
		
		vTaskDelay(pdMS_TO_TICKS(10));
	}
	
	BODYPRESCTRL.presCtrlEn = 0;
	
	//关闭电磁阀和泵
	SWITCH_ON_OFF(IO_20, SWITCH_OFF);
	SWITCH_ON_OFF(IO_13, SWITCH_OFF);
	SWITCH_ON_OFF(IO_05, SWITCH_OFF);
	SWITCH_ON_OFF(IO_11, SWITCH_OFF);
	
	s_pfc_state = PFC_STATE_IDLE;
	
	return;
	
#endif ///
}

/*---------------------------------------------------------------------------------------
 函数原型: void PFC_Thread(void *pvParameters)
 功    能: 静态法主流程控制线程执行函数
 输入参数: NA
 返 回 值: NA
 注意事项: 	
---------------------------------------------------------------------------------------*/
void PFC_Thread(void *pvParameters)
{
	s_pfc_state = PFC_STATE_POST;
	//s_pfc_state = PFC_STATE_DEBUG;
	
    while(1)
    {
		switch(s_pfc_state)
        {
			case PFC_STATE_POST:
                PFC_StatePost();
                break;
			case PFC_STATE_IDLE:
                PFC_StateIdle();
                break;
			case PFC_STATE_RUN:
                PFC_StateRun();
                break;
            case PFC_STATE_ERROR:
                PFC_StateError();
                break;
			case PFC_STATE_DEBUG:
				PFC_StateDebug();
			  break;
        }
    }  
}

/***********************************************************************************
 * @brief 静态法主流程控制线程初始化
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void PFC_Init(void)
{
	//初始化参数
	
}

/******************* (C) COPYRIGHT 2022 CIQTEK luffy *****END OF FILE****/
